{% extends "base.html" %}

{% block title %}
    {{ main_top_info }}
{% endblock %}

{% block main_content %}
    <div class="container index">
  <div class="row">
    <div class="col-xs-12 main">
      <!-- 用户title部分导航 -->
       <div class="main-top">
          <h4>{{ main_top_info }}({{ count }})</h4>
      </div>
       <hr>
      <div id="list-container">
        <!-- 文章列表模块 -->
        <ul class="note-list public-article-list">
        {% include 'public-actions-list-ajax.html' %}
        </ul>
        <!-- 文章列表模块 -->
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block custom_js %}
<script type="text/javascript">
    /* 关注话题 */
    function followTopicPublicList(tid) {
        {% if not request.user.is_authenticated %}
            tips('请登录后再操作！', 'danger');
        {% endif %}
        var action =  $("#attenttotopic_"+tid).data('action');
        $.ajax({
            cache: false,
            type: "POST",
            url: "{% url 'users:follow_topic' %}",
            data: {'tid': tid, 'action': action},
            async: true,
            beforeSend:function(xhr, settings){
                xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
            },
            success: function(data) {
                if (data['msg'] == 'ok') {
                    $("#attenttotopic_"+tid).data('action', action == 'follow' ? 'unfollow' : 'follow');
                    if ($("#attenttotopic_"+tid).hasClass("btn-default")) {
                        $("#attenttotopic_"+tid).removeClass("btn-round btn-default");
                        $("#attenttotopic_"+tid).addClass("btn-teal btn-circular");

                        $("#attenttotopic_"+tid).html('<i class="fa fa-plus"></i> 关注');
                    } else {
                        $("#attenttotopic_"+tid).removeClass("btn-teal btn-circular");
                        $("#attenttotopic_"+tid).addClass("btn-round btn-default");

                        $("#attenttotopic_"+tid).html('<i class="fa fa-check"></i> 已关注');
                    }
                }
            },
        });
    }
</script>
<script type="text/javascript">
    $.ajaxSetup({
        data: { csrfmiddlewaretoken: '{{ csrf_token }}' },
    });
    $(document).ready(function () {
        var page = 1;
        var empty_page = false;
        var block_request = false;

        $(window).scroll(function() {
        var margin = $(document).height() - $(window).height() - 200;
            if  ($(window).scrollTop() > margin && empty_page == false && block_request == false) {
                {% if count < 10 %}
                    return false;
                {% endif %}
                block_request = true;
                page += 1;
                $.get('?page=' + page, function(data) {
                    if(data == '')
                    {
                        empty_page = true;
                    }
                    else {
                        block_request = false;
                        $('.public-article-list').append(data);
                    }
                });
            }
        });
    });
</script>
{% endblock %}